<?php
/*
 * Copyright (c) 2009 Lukasz Sarzynski [ http://lukasz.sarzynski.biz ]
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 *
 * @author     Lukasz Sarzynski <LukaszSarzynski@gmail.com>
 *
 */
class BaseCaPropelTreeActions extends sfActions
{
  /**
   * Change culture
   * @param <type> $request
   * @return <type>
   */
  public function executeChangeCulture($request)
  {
    $sCulture = ($this->getRequest()->getParameter('culture'));
    $this->getUser()->setAttribute('sPropelTreeCulture', $sCulture);
    $this->getResponse()->setContent('
      <script type="text/javascript">$(document).ready(function(){$(\'#ca_pnsm_tree\').load(\''.$this->generateUrl('ca_get_all_tree').'\');$(\'#ca_pnsm_main_menu\').load(\''.$this->generateUrl('ca_get_culture_main_menu').'\');});</script>'
      .$this->getContext()->getI18N()->__('change culture to: "%1%"', array('%1%' => $sCulture))
      );

    return sfView::NONE;
  }

  /**
   * Get all tree
   * @param <type> $request
   */
  public function executeGetAllTree($request)
  {
    $peer = sprintf('%sPeer', sfConfig::get('app_caPropelTreePlugin_sModelNameclass'));
    $this->oRoot = call_user_func(array($peer, 'retrieveRoot'));
    $this->setLayout(false);
  }

  /**
   * get culture menu is need width user change localization
   * @param <type> $request
   * @return <type>
   */
  public function executeGetCutureMainMenu($request)
  {
      $this->setLayout(false);
  }

  /**
   * Get tree part node
   * @note need parameter node_id
   * @param <type> $request
   */
  public function executeGetTreeChildren($request)
  {
    $this->id = (int)($this->getRequest()->getParameter('node_id'));
    $peer = sprintf('%sPeer', sfConfig::get('app_caPropelTreePlugin_sModelNameclass'));
    $this->oNode = call_user_func(array($peer, 'retrieveByPk'), $this->id);
    if($this->oNode)
    {
      $this->iParentId = call_user_func(array($this->oNode, sfConfig::get('app_caPropelTreePlugin_sObjectIdMethod')));

      $this->setLayout(false);
    }
    else
    {
      $this->getResponse()->setContent(
        $this->getContext()->getI18N()->__('this node is not isset')
        );
      return sfView::NONE;
    }
  }

  /**
   * Add id node to cliboard
   * @note need parameter node_id
   * @param <type> $request
   */
  public function executePushNode($request)
  {
    $iNodeId = (int)($this->getRequest()->getParameter('node_id'));

    $peer = sprintf('%sPeer', sfConfig::get('app_caPropelTreePlugin_sModelNameclass'));
    $oNode = call_user_func(array($peer, 'retrieveByPk'), $iNodeId);
    if($oNode)
    {
      $this->getUser()->setAttribute('iPushNodeId', $iNodeId);
      $oNodeName = call_user_func(array($oNode, sfConfig::get('app_caPropelTreePlugin_sObjectNameWidthCultureMethod')),$this->getUser()->getAttribute('sPropelTreeCulture'));
      $this->getResponse()->setContent(
        $this->getContext()->getI18N()->__('node "%1%" add to clipboard', array('%1%' => $oNodeName))
        );
    }
    else
    {
      $this->getResponse()->setContent(
        $this->getContext()->getI18N()->__('this node is not isset')
        );
    }
    return sfView::NONE;
  }

  /**
   * Put node from cliboard after node in parameter
   * @note need parameter node_id
   * @param <type> $request
   */
  public function executePutAfterNode($request)
  {
    return $this->putNodeBefore(false);
  }

  /**
   * Put node from cliboard before node in parameter
   * @note need parameter node_id
   * @param <type> $request
   */
  public function executePutBeforeNode($request)
  {
    return $this->putNodeBefore(true);
  }

  /**
   * Put node before or after node from cliboard
   * @param bool $bPutBeforeNode if is true node was put before node from cliboard
   * @return <type>
   */
  private function putNodeBefore($bPutBeforeNode = true)
  {
    $iPutNodeId = (int)($this->getRequest()->getParameter('node_id'));
    $iPushNodeId = $this->getUser()->getAttribute('iPushNodeId', false);

    if($iPushNodeId && $iPushNodeId)
    {
      $peer = sprintf('%sPeer', sfConfig::get('app_caPropelTreePlugin_sModelNameclass'));
      $oNodePut = call_user_func(array($peer, 'retrieveByPk'), $iPutNodeId);
      $oNodePush = call_user_func(array($peer, 'retrieveByPk'), $iPushNodeId);
      $oNodePutName = call_user_func(array($oNodePut, sfConfig::get('app_caPropelTreePlugin_sObjectNameWidthCultureMethod')),$this->getUser()->getAttribute('sPropelTreeCulture'));
      $oNodePushName = call_user_func(array($oNodePush, sfConfig::get('app_caPropelTreePlugin_sObjectNameWidthCultureMethod')),$this->getUser()->getAttribute('sPropelTreeCulture'));

      if( ($oNodePut) && ($oNodePush) )
      {
        if($bPutBeforeNode)
        {
          $oNodePush->moveToNextSiblingOf($oNodePut);
          $this->getResponse()->setContent(
            $this->getContext()->getI18N()->__('sucess put node "%1%" before  node "%2%"' , array('%1%' => $oNodePushName,'%2%' => $oNodePutName))
            );
        }
        else
        {
          $oNodePush->moveToPrevSiblingOf($oNodePut);
          $this->getResponse()->setContent(
              $this->getContext()->getI18N()->__('sucess put node "%1%" after  node "%2%"' , array('%1%' => $oNodePushName,'%2%' => $oNodePutName))
              );
        }
      }
      else
      {
        $this->getResponse()->setContent(
          $this->getContext()->getI18N()->__('some node is not isset')
          );
      }
    }
    else
    {
      $this->getResponse()->setContent(
        $this->getContext()->getI18N()->__('some node is not isset')
        );
    }
    //clear atributes
    $this->getUser()->setAttribute('iPushNodeId', false);
    
    return sfView::NONE;
  }

  /**
   * Add node to parent
   * @note need parameter parent_node_id
   * @param <type> $request
   * @return <type>
   */
  public function executeAddNodeToParent($request)
  {
    $this->iParentNodeId = (int)($this->getRequest()->getParameter('parent_node_id'));

    $sFormName = sfConfig::get('app_caPropelTreePlugin_sFormCreateNodeName');
    $this->oForm = new $sFormName();

    if ($request->isMethod('post'))
    {
      if ( ($request->isMethod('post')) && ($this->oForm->bindAndSave($request->getParameter($this->oForm->getName()), $request->getFiles($this->oForm->getName()))) )
      {
        $sUserCulture = $this->getUser()->getAttribute('sPropelTreeCulture');
        $peer = sprintf('%sPeer', sfConfig::get('app_caPropelTreePlugin_sModelNameclass'));
        $oParentNode = call_user_func(array($peer, 'retrieveByPk'), $this->iParentNodeId);
        $oNewNode = $this->oForm->getObject();
        //add node to parent
        call_user_func(array($peer, 'moveToLastChildOf'), $oParentNode, $oNewNode);
        //get parent id
        $iParentNodeId = call_user_func(array($oParentNode, sfConfig::get('app_caPropelTreePlugin_sObjectIdMethod')));
        //get parent name
        $sParentNodeName = call_user_func(array($oParentNode, sfConfig::get('app_caPropelTreePlugin_sObjectNameWidthCultureMethod')),$sUserCulture);
        //get node name 
        $sNewNodeName = call_user_func(array($oNewNode, sfConfig::get('app_caPropelTreePlugin_sObjectNameWidthCultureMethod')),$sUserCulture);

        $peer = sprintf('%sPeer', sfConfig::get('app_caPropelTreePlugin_sModelNameclass'));
        $oRoot = call_user_func(array($peer, 'retrieveRoot'));
        $iRootId = call_user_func(array($oRoot, sfConfig::get('app_caPropelTreePlugin_sObjectIdMethod')));
        if($iRootId == $iParentNodeId)
        {
          $this->getResponse()->setContent('
            <script type="text/javascript">$(document).ready(function(){$(\'#ca_pnsm_tree\').load(\''.$this->generateUrl('ca_get_all_tree').'\');});</script>'
            .$this->getContext()->getI18N()->__('node "%1%" succes add to parent "%2%"' , array('%1%' => $sNewNodeName,'%2%' => $sParentNodeName))
          );
        }
        else
        {
          $this->getResponse()->setContent('
            <script type="text/javascript">$(document).ready(function(){$(\'#node_children_'.$iParentNodeId .'\').load(\''.$this->generateUrl('ca_get_tree',array('node_id' =>  $iParentNodeId  )).'\').show();$(\'#add_'.$iParentNodeId .'\').show();$(\'#node_'.$iParentNodeId .'\').parent().removeClass("folder-close").removeClass("doc").addClass("folder-open");$(\'#node_children_'.$iParentNodeId .'\').removeClass("not-load");});</script>'
            .$this->getContext()->getI18N()->__('node "%1%" succes add to parent "%2%"' , array('%1%' => $sNewNodeName,'%2%' => $sParentNodeName))
          );
        }

        
        return sfView::NONE;
      }
    }
  }

  /**
   * Deleted node;
   * @note need parameter node_id
   * @param <type> $request
   * @return string
   */
  public function executeDeleteNode($request)
  {
    $iNodeId = (int)($this->getRequest()->getParameter('node_id'));
    $peer = sprintf('%sPeer', sfConfig::get('app_caPropelTreePlugin_sModelNameclass'));
    $oNode = call_user_func(array($peer, 'retrieveByPk'), $iNodeId);
    if($oNode)
    {
      $sNodeName = call_user_func(array($oNode, sfConfig::get('app_caPropelTreePlugin_sObjectNameWidthCultureMethod')),$this->getUser()->getAttribute('sPropelTreeCulture'));
      $oNode->delete();
      $this->getResponse()->setContent(
        $this->getContext()->getI18N()->__('node "%1%" deletet', array('%1%' => $sNodeName))
        );
    }
    else
    {
      $this->getResponse()->setContent(
        $this->getContext()->getI18N()->__('this node is not isset')
        );
    }
    return sfView::NONE;
  }


  /**
   * Edit node
   * @note need parameter node_id
   * @param <type> $request
   * @return <type>
   */
  public function executeRenameNode($request)
  {
    $this->iNodeId = (int)($this->getRequest()->getParameter('node_id'));

    $peer = sprintf('%sPeer', sfConfig::get('app_caPropelTreePlugin_sModelNameclass'));
    $oNode = call_user_func(array($peer, 'retrieveByPk'), $this->iNodeId);

    if($oNode)
    {
      $sFormName = sfConfig::get('app_caPropelTreePlugin_sFormEditNodeName');
      $this->oForm = new $sFormName($oNode);

      if ($request->isMethod('post'))
      {
        $this->oForm->bind($request->getParameter($this->oForm->getName()), $request->getFiles($this->oForm->getName()));
        if ($this->oForm->isValid())
        {
          $this->oForm->save();
          $oNode = $this->oForm->getObject();
          $oNodeName = call_user_func(array($oNode, sfConfig::get('app_caPropelTreePlugin_sObjectNameWidthCultureMethod')),$this->getUser()->getAttribute('sPropelTreeCulture'));


          $oParentNode = $oNode->retrieveParent();
          
          $iParentNodeId = call_user_func(array($oParentNode, sfConfig::get('app_caPropelTreePlugin_sObjectIdMethod')));

          $oRoot = call_user_func(array($peer, 'retrieveRoot'));
          $iRootId = call_user_func(array($oRoot, sfConfig::get('app_caPropelTreePlugin_sObjectIdMethod')));
          if($iRootId == $iParentNodeId)
          {
            $this->getResponse()->setContent('
              <script type="text/javascript">$(document).ready(function(){$(\'#ca_pnsm_tree\').load(\''.$this->generateUrl('ca_get_all_tree').'\');});</script>'
              .'<script type="text/javascript">$(document).ready(function(){$(\'#node_'.$this->iNodeId.'\').text(\''.$oNodeName.'\');$(\'#edit_'.$this->iNodeId.'\').show();});</script>'
              .$this->getContext()->getI18N()->__('node "%1%" succes rename' , array('%1%' => $oNodeName))
            );
          }
          else
          {
            $this->getResponse()->setContent('
              <script type="text/javascript">$(document).ready(function(){$(\'#node_children_'.$iParentNodeId .'\').load(\''.$this->generateUrl('ca_get_tree',array('node_id' =>  $iParentNodeId  )).'\').show();$(\'#add_'.$iParentNodeId .'\').show();$(\'#node_'.$iParentNodeId .'\').parent().removeClass("folder-close").removeClass("doc").addClass("folder-open");$(\'#node_children_'.$iParentNodeId .'\').removeClass("not-load");});</script>'
              .$this->getContext()->getI18N()->__('node "%1%" succes add to parent "%2%"' , array('%1%' => $sNewNodeName,'%2%' => $sParentNodeName))
            );
          }

          return sfView::NONE;
        }
      }
    }
    else
    {
      $this->getResponse()->setContent(
        $this->getContext()->getI18N()->__('this node is not isset')
        );
      return sfView::NONE;
    }
  }

}